/**
 * @Author       : Hejh(3010733382@qq.com)
 * @Version      : V1.0
 * @Date         : 2024-01-19 11:21:56
 * @Description  : 日志记录工具类
 */

const { SystemLog } = require('@/framework/mongoose')

/**
 * @author: Hejh(3010733382@qq.com)
 * @description: 用于记录日志的方法
 * @param {*} type 日志类型
 * @param {*} log 具体日志描述
 */
module.exports.logRecord = async (ctx, type, log) => {
  const { ip } = ctx.request
  const newlog = new SystemLog({
    ip,
    ua: ctx.request.header['user-agent'],
    userId: ctx?.user?.id || '',
    username: ctx?.user?.username || '',
    type,
    ...log
  })
  try {
    await newlog.save()
  } catch (e) {
    console.log(e)
  }
}
